package com.glsc.ngateway.filetransfer.util;


import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author gaojc
 * @date 2020/8/26 10:21
 */

@Component
public class TokenUtil {

    @Value("${sign.secretkey}")
    private String secretkey;

    /**
     * 功能描述: 根据playLoad生成token<br>
     * <>
     *
     * @Param: [playLoad]
     * @Return: String
     * @Author: gaojc
     * @Date: 2020/8/28 14:56
     */
    public String generateToken(String playLoad) {
        SecretKey secretKey1 = new SecretKeySpec(secretkey.getBytes(), SignatureAlgorithm.HS256.getJcaName());
        //设置header信息
        Map<String, Object> header = new LinkedHashMap<>();
        header.put("alg", "HS256");
        header.put("typ", "JWT");

        String jws = Jwts.builder().setHeader(header).setPayload(playLoad).signWith(SignatureAlgorithm.HS256, secretKey1).compact();
        return jws;
    }

}
